define(function (require, exports, module) {
    var template = require("template");
    var Ajax = require("ajax");
    var layer = require("layer");
    var pagination = require('pagination');
    var util = require('util');
    
    var negativeObj = function (callback) {
        this.init.apply(this,arguments);
    };
    negativeObj.prototype = {
        constructor: negativeObj,
        options: {},
        init: function (options) {
            var t = this;
            t.options = $.extend({},t.options,options);
            $(".tipConNav").text("负面舆情");
            $(window).off('scroll');
            $("#rightConBox").html(template("monitorTemp",{}));
            t.mask();
            t.querySimilar();
            t.loadInitList(1);
            t.isCollectionEvent();
            t.loadWeixinEvent();
        },
        mask: function(){
            $(".mask").css("height",$(document).height());
            $(".mask").css("width",$(document).width());
        },
        loadInitList: function(pageIndex) {
			var t = this;
			Ajax.ajax({
				url: localhost + "Warning/findNegativeMore.do",
				data: {
					pageIndex: pageIndex,
					lessForward:10
				},
				beforeSend: function() {
					if(pageIndex==1){
						$('#negative_table>tbody').html(template("loadingDom"));
					}else{
						$("#listDom").append("<div class='loadingMoreBox animated fadeIn nextloding'><span>正在加载中，请稍后</span></div>");
					}
				},
				success: function(data) {
    				//防止失效的请求影响到下次页面的滚动加载
    				var href = window.location.href.indexOf("admin/home_negative");
    				if(href < 0 ) {
    					return;
    				}
					if(data.returnCode == 10000){
						if(data.returnData && data.returnData.length > 0) {
							$(".loadingBox").remove();
							$(".loadingMoreBox").remove();
							var tHead='',tList='',isCollection='',sourceName='',level='',colors='',
								dataList = data.returnData;
							
							tHead = "<tr class='th'><th>标题</th><th>来源</th><th>作者</th><th>时间</th><th>转载数</th><th>星级</th><th>收藏</th></tr>";
			                for (var i = 0; i < dataList.length; i++) {
			                    var item = dataList[i]; 
			                    //表格
			                    tList += '<tr data-id="'+ item.ifmId +'" class="'+ item.groupId +'" data-class="'+ item.groupId +'">';
			                    /*tList+='<td><i class="iconfont checkboxChange">&#xe6dc;</i><input data-id="'+ item.id +'" type="checkbox" class="checkItem"></td>';*/
			                    if(item.articleType=="WeiXin"){
			                    	if(localStorage.getItem(item.articleId)!=null){
			                    		tList += '<td><a class="title changeColor" data-type="weixin" data-ifmId="'+ item.ifmId +'" data-articleId="'+ item.articleId +'" href="javascript:;">'+item.title+'</a></td>';
			                    	}else{
			                    		tList += '<td><a class="title" data-type="weixin" data-ifmId="'+ item.ifmId +'" data-articleId="'+ item.articleId +'" href="javascript:;">'+item.title+'</a></td>';
			                    	}
			                    }else{
			                    	tList += '<td><a class="title" target="_blank" href="' + item.url + '">'+item.title+'</a></td>';
			                    }
			                    
			                    if(item.articleType=="News"){
			                    	sourceName = item.websiteName;
			                    }else if(item.articleType=="Micro"){
			                    	sourceName = "微博";
			                    }else if(item.articleType=="BBS"){
			                    	sourceName = "论坛";
			                    }else if(item.articleType=="WeiXin"){
			                    	sourceName = "微信";
			                    }
			                    tList += '<td>'+sourceName+'</td>';
			                    tList += '<td>'+item.author+'</td>';
			                    tList += '<td>'+item.releaseTime+'</td>';
			                    
			                    if (item.reprintNumb <= 1) {
			                    	tList += '<td class="similar">无转载<ul class="similar_ul"></ul></td>';
			                    }else if(item.reprintNumb<100){
//			                        if(item.articleType == "Micro") {
//			                        	tList += '<td class="similar"><b>相似'+item.reprintNumb+'</b><div class="triangle_up"></div><ul class="similar_ul"></ul></td>';
//			                        } else {
//			                        	tList += '<td class="similar"><b>转载'+item.reprintNumb+'</b><div class="triangle_up"></div><ul class="similar_ul"></ul></td>';
//			                        }
			                    	tList += '<td class="similar"><b>转载'+item.reprintNumb+'</b><div class="triangle_up"></div><ul class="similar_ul"></ul></td>';
			                    }else {
			                    	tList += '<td class="similar"><b>转载99+</b><div class="triangle_up"></div><ul class="similar_ul"></ul></td>';
//			                        if(item.articleType == "Micro") {
//			                        	tList += '<td class="similar"><b>相似99+</b><div class="triangle_up"></div><ul class="similar_ul"></ul></td>';
//			                        } else {
//			                        	tList += '<td class="similar"><b>转载99+</b><div class="triangle_up"></div><ul class="similar_ul"></ul></td>';
//			                        }
			                    }
			                    if(!item.starLevel){
			                    	level = "";
			                    }else{
			                    	level = item.starLevel;
			                    }
			                    tList += '<td>'+level+'</td>';
			                    if(item.isCollection==0){
			                    	isCollection = "收藏";
			                    	colors = "black";
			                    }else{
			                    	isCollection = "已收藏";
			                    	colors = "grey";
			                    }
			                    
			                    tList += '<td><a href="javascript:;" ';
			                    tList += isNull(item.isCollection,'data-isCollection=');
								tList += isNull(item.nature,'data-nature=');
								tList += isNull(item.ifmId,'data-ifmid=');
								tList += isNull(item.articleId,'data-articleid=');
								tList += isNull(sourceName,'data-articleType=');
								tList += isNull(item.author,'data-enshrineAuthor=');
								tList += isNull(item.title,'data-enshrineTitle=');
								tList += isNull(item.url,'data-enshrineUrl=');
								tList += isNull(item.releaseTime,'data-releaseTime1=');
								tList += isNull(level,'data-starLevel=');
								tList += isNull(item.reprintNumb,'data-reprintNum=');
								
			                    tList += ' class="collect '+ colors +'">'+isCollection+'</a></td>';
			                    tList += '</tr>';
			                }
			                
							//如果属性值为空，那么此元素就不添加该属性
							function isNull(isText,data){
								if(isText||isText=='0'){
									var tL = data + "'"+isText+"'";
								}else{
									tL = '';
								}
								return tL;
							}
			                if(pageIndex==1){
			                	$('#negative_table>tbody').html('');
			                	$('#negative_table>tbody').append(tHead+tList);
			                }else{
			                	$('#negative_table>tbody').append(tList);
			                }
//			                util.CutOff();      //截断过长文字
			                if(href > -1 ) {
			                	t.loadPage(pageIndex);
			                }
		
						} else {
							$(".loadingBox").remove();
							$(".loadingMoreBox").remove();
							$(window).off('scroll');
							if(pageIndex==1){
								$('#negative_table>tbody').html(template("noDataDom"));
							}
						}
					}
				}
			});
		},
		loadPage: function(pageIndex) {
			var t = this;
			//获取页面顶部被卷起来的高度
			function scrollTop() {
				return Math.max(
					//chrome
					document.body.scrollTop,
					//firefox/IE
					document.documentElement.scrollTop);
			}
			//获取页面文档的总高度
			function documentHeight() {
				//现代浏览器（IE9+和其他浏览器）和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以
				return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
			}
			function windowHeight() {
				return(document.compatMode == "CSS1Compat") ?
					document.documentElement.clientHeight :
					document.body.clientHeight;
			}
			if(window.location.href.indexOf("admin/home_negative")>-1){
			}else{
				return false;
			}
			var isloading = false;
			$(window).off('scroll');
			$(window).on('scroll', function() {
				//判定是否加载中，防止重复加载
				if(!isloading){
					//滚动条卷起来的高度 + 窗口高度 >= 文档的总高度 - 50/*这里将滚动响应区域高度取50px*/；如果这个判断为true则表示滚动条滚动到了底部。
					if(scrollTop() + windowHeight() >= (documentHeight() - 50)) {
						isloading = false;
						pageIndex = parseInt(pageIndex)+ 1;
						if(window.location.href.indexOf("admin/home_negative")>-1){
						}else{
							return false;
						}
						t.loadInitList(pageIndex);
						isloading = true;
					}
				}
			});
		},
		
        //查询相似信息
        querySimilar: function(){
            var count =0;
            $('#negative_table>tbody').delegate('b','click',function(){
                if($(this).next().css('display') == 'none') {
                    count = 0;
                }else{
                	count = 1;
                }
                count++;
                if(count%2 ==0) { //关闭
                	$('.triangle_up').css('display','none');    //小三角
                    $('.similar_ul').css('display','none');    //隐藏其他弹框
                    return false;
                } else { //打开
                    $(this).next().next().empty();  //$(".similar_ul")
                    $(this).parents('tr').siblings().children().children('.triangle_up').css('display','none');    //小三角
                    $(this).parents('tr').siblings().children().children('ul').css('display','none');    //隐藏其他弹框
                    var ifmId = $(this).parents('tr').attr('data-id');
                    var groupId = $(this).parents('tr').attr('data-class');
                    var $this = $(this);
                    Ajax.ajax({
                        url: localhost + "Warning/findSimilarity.do",
                        type: 'post',
                        dataType: 'json',
                        data: {groupId: groupId, ifmId: ifmId, articleType: "Micro,BBS,WeiXin",identifying:1},
                        contentType : "application/x-www-form-urlencoded; charset=utf-8",
                        success: function (data) {
                            if(data.returnData.length>0) {
                            	var sourceNames='',tLists='';
                                for (var i =0;i< data.returnData.length; i++) {
                                    var dataSimilar = data.returnData[i];
    			                    
    			                    if(dataSimilar.articleType=="News"){
    			                    	sourceNames = dataSimilar.websiteName;
    			                    }else if(dataSimilar.articleType=="Micro"){
    			                    	sourceNames = "微博";
    			                    }else if(dataSimilar.articleType=="BBS"){
    			                    	sourceNames = "论坛";
    			                    }else if(dataSimilar.articleType=="WeiXin"){
    			                    	sourceNames = "微信";
    			                    }
    			                    //判断是否弹出浮层
    			                    if(dataSimilar.articleType=="WeiXin"){
    			                    	tLists = '<li class="similar_ul"><a data-type="weixin" data-ifmId="'+ dataSimilar.ifmId +'" data-articleId="'+ dataSimilar.articleId +'" href="javascript:;">'+dataSimilar.title + '';
    			                    }else{
    			                    	tLists = '<li class="similar_ul"><a class="similar_i" target="_blank" href="' + dataSimilar.url + '">' + dataSimilar.title + '';
    			                    }
                                    tLists += '</a><a href="javascript:void(0)" class="similar_i">'+ sourceNames +
                                    		  '</a><a href="javascript:void(0)" class="similar_i">'+ dataSimilar.author +
                                    		  '</a><a href="javascript:void(0)" class="similar_i">'+dataSimilar.releaseTime +'</a></li>';
                                    
                                    $this.next().next().append(tLists);
                                }
                                $this.parents('tr').css('position','relative');
                                $this.next().next().css('display','block');
                                $this.next().css('display','block');
                            }
                        }
                    })
                }
            });
            $('.body_home_negative').click(function(e){
                if(e.target.className == 'similar_ul' || e.target.className == 'similar_i') {
                
                } else {
                    $('.triangle_up').css('display','none');
                    $('.similar_ul').css('display','none');
                }
            });
        },
        
        //收藏
        isCollectionEvent: function(){
        	$('.monitor_table > tbody').delegate('.collect','click',function(){
        		$(this).removeClass('collect');
        		var t = $(this);
        		if($(this).attr("data-iscollection")==1){
        			return false;
        		}
        		var articleId = $(this).attr("data-articleid");
        		var ifmId = $(this).attr("data-ifmid");
        		var articleType = $(this).attr("data-articleType");
        		var enshrineAuthor = $(this).attr("data-enshrineAuthor");
        		var enshrineTitle = $(this).attr("data-enshrineTitle");
        		var enshrineUrl = $(this).attr("data-enshrineUrl");
        		var releaseTime1 = $(this).attr("data-releaseTime1");
        		var reprintNum = $(this).attr("data-reprintNum");
        		var params = {
        			articleId: articleId,
        			articleType: articleType,
        			enshrineAuthor: enshrineAuthor,
        			enshrineTitle: enshrineTitle,
        			enshrineUrl: enshrineUrl,
        			releaseTime1: releaseTime1,
        			ifmId: ifmId,
        			reprInt:reprintNum
        		}
    			Ajax.ajax({
    				url: localhost + "enshrine/insert.do",
    				data: params,
    				success: function(data) {
    					if(data.returnCode == 10000){
    						layer.msg(data.returnMessage,{time: 2000},function(){
    							t.attr("data-iscollection",1).text("已收藏").css({"color":"#7B8FA6","text-decoration":"none"});
    						});
    					}
    				}
    			});
        	});
        },
        //微信操作
        loadWeixinEvent:function(){
            var t =this;
            $('.monitor_table>tbody').delegate('a', 'click', function () {
                var type = $(this).attr('data-type');
                if(type =="weixin"){
                    if(!$(this).hasClass("changeColor")){
                    	$(this).addClass("changeColor");
                    }
                	
                	var articleId = $(this).attr('data-articleId');
                	var ifmId = $(this).attr('data-ifmId');
                	
                	var params ={
                		articleId:articleId,
                		ifmId:ifmId
                	}
                	Ajax.ajax({
        				url: localhost + "Monitoring/findSingle.do",
        				data: params,
        				success: function(data) {
        					if(data.returnCode == 10000){
        	                    var title = data.returnData.title;
        	                    var author = data.returnData.author;
        	                    var releaseTime = data.returnData.releaseTime;
        	                    var source = data.returnData.source;
        	                    var content = data.returnData.content;
        	                    var reg = /\n/g;
        	                    content = content.replace(reg, '<br>');
        	                    layer.open({
        	                        title: '&nbsp;',
        	                        type: 1,
        	                        closeBtn: 1,
        	                        skin: 'inner_url',
        	                        content: "<div class='weixin_title'>" + title + "</div>" +"<div class='weixin_source'><span>"+ releaseTime+"</span><span>"+ author +"</span><span>"+ source+"</span></div><div class='weixin_content'>"+ content + "</div>",
        	                        area: ['80%', '650px'],
        	                        btn:['关闭'],
        	                        maxmin: false,
        	                        scrollbar: false, // 父页面 滚动条 禁止
        	                        shadeClose: true
        	                    });
        	                    localStorage.setItem(articleId, new Date().valueOf());
        					}
        				}
        			});
                }
            });
        }
    };
    module.exports = negativeObj;
});

